package lxx.zhihudailypaper.activity;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;

import lxx.zhihudailypaper.R;
import lxx.zhihudailypaper.bean.LoadViewBean;
import lxx.zhihudailypaper.util.StreamUtils;
import lxx.zhihudailypaper.view.MyImageView;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

	private String text;
	private String img;
	private String tag = "MainActivity";
	private MyImageView iv_bg;
	private Button bt_download;
	private Context ctx;

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			LoadViewBean lvBean = (LoadViewBean) msg.obj;
			if (lvBean != null) {
				Log.d(tag, "handler=======");
				String img = lvBean.getImg();

				iv_bg.setImageUrl(img);
			}
		};
	};
	
	private Handler handler2 = new Handler(){
		public void handleMessage(Message msg) {
			switch(msg.what){
				case 500:
					enterReadActivity();
					break;
			}
		};
	};
	private Button bt_toRead;
	private LinearLayout ly_root;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		ctx = this;

		initUI();
		initData();
		initAnimation();
	}

	private void initAnimation() {
		AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
		alphaAnimation.setDuration(3000);
		ly_root.startAnimation(alphaAnimation);
	}

	protected void enterReadActivity() {
		Intent intent = new Intent(this,ReadActivity.class);
		startActivity(intent);
		finish();
		
	}

	private void initUI() {
		iv_bg = (MyImageView) findViewById(R.id.iv_bg);
		ly_root = (LinearLayout) findViewById(R.id.ly_root);
	}

	private void initData() {
		
		new Thread(){
			public void run() {
				try{
					URL url = new URL("http://news-at.zhihu.com/api/4/start-image/1080*1776");
					HttpURLConnection conn = (HttpURLConnection) url.openConnection();
					conn.setRequestMethod("GET"); 
					conn.setConnectTimeout(10*1000); 
					int rcode = conn.getResponseCode();
					if(rcode == 200){
						InputStream inputStream = conn.getInputStream();
						String sts = StreamUtils.streamToString(inputStream); 
						Log.i(tag, sts);
						JSONObject json = new JSONObject(sts);
						LoadViewBean bean = new LoadViewBean();
						bean.setText(json.getString("text"));
						bean.setImg(json.getString("img"));
						 
						 Message msg = Message.obtain(); 
						 msg.obj = bean; 
						 handler.sendMessage(msg);
						 //handler2.sendMessageDelayed(msg, 5000);
						 handler2.sendEmptyMessageDelayed(500, 5000);
					}
				}catch(Exception e){
					
				}
				
				
			};
		}.start();
		 
		
		 
	}

}


 /** bt_toRead = (Button) findViewById(R.id.bt_toRead);
 * bt_toRead.setOnClickListener(new OnClickListener() {
 * 
 * public void onClick(View v) { Intent intent = new
 * Intent(getApplicationContext(),ReadActivity.class); startActivity(intent); }
 * });
 * 
 * 
 * bt_download.setOnClickListener(new OnClickListener() {
 * 
 * public void onClick(View v) { new Thread(){ public void run() { final
 * loadViewBean lvBean = new loadViewBean(); try {
 * //http://news-at.zhihu.com/api/4/start-image/1080*1776
 * 
 * URL url = new URL("http://news-at.zhihu.com/api/4/start-image/1080*1776");
 * 
 * HttpURLConnection conn = (HttpURLConnection) url.openConnection();
 * conn.setRequestMethod("GET"); conn.setConnectTimeout(10*1000); int rcode =
 * conn.getResponseCode();
 * 
 * if(rcode == 200){ InputStream inputStream = conn.getInputStream(); String sts
 * = StreamUtils.streamToString(inputStream); Log.i(tag, sts); try { JSONObject
 * json = new JSONObject(sts); lvBean.setText(json.getString("text"));
 * lvBean.setImg(json.getString("img"));
 * 
 * Message msg = Message.obtain(); msg.obj = lvBean; handler.sendMessage(msg); }
 * catch (JSONException e) { e.printStackTrace(); }
 * 
 * } } catch (MalformedURLException e) { e.printStackTrace(); } catch
 * (IOException e) { e.printStackTrace(); } }; }.start(); Toast.makeText(ctx,
 * "123465", Toast.LENGTH_SHORT).show(); } });
 */